package com.jay.media.music.ui.play

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.jay.base.ui.BaseActivity
import com.jay.media.kit.GsonUtils
import com.jay.media.kit.LogHelper
import com.jay.media.music.core.constant.Actions
import com.jay.media.music.core.constant.Keys
import com.jay.media.music.core.constant.MusicListType
import com.jay.media.music.core.db.Music
import com.jay.media.music.ui.R
import com.jay.media.music.ui.manager.DefaultMusicListener
import com.jay.media.music.ui.manager.MusicClientManager

/**
 * @author xuzhou4
 * @date 2022/2/15
 */
class MusicListActivity : BaseActivity() {

    companion object {
        fun start(context: Context, type: Int) {
            val intent = Intent(context, MusicListActivity::class.java)
            intent.putExtra(MusicListType.TYPE_KEY, type)
            context.startActivity(intent)
        }
    }

    private val viewModel: PlayHistoryViewModel by viewModels(null)
    private var playHistoryAdapter: MusicListAdapter? = null
    private var playListType = MusicListType.TYPE_PLAY_LIST

    private var mMusicListener = object : DefaultMusicListener() {
        override fun onSessionEvent(event: String, extras: Bundle?) {
            LogHelper.d(TAG, "onMediaEvent() called with: event = $event, extras = $extras")
            if (event == Actions.ACTION_QUERY_MUSIC_LIST) {
                val json = extras?.getString(Keys.KEY_MUSIC_LIST) ?: ""
                viewModel.setPlayHistoryMusicList(
                    GsonUtils.fromJsonArray(
                        json,
                        Music::class.java
                    )
                )
            }
        }
    }

    override fun layoutId(): Int = R.layout.activity_play_history

    @SuppressLint("NotifyDataSetChanged")
    override fun initView(view: View) {
        playListType = intent.getIntExtra(MusicListType.TYPE_KEY, MusicListType.TYPE_PLAY_LIST)
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
        setToolbarTitle(getToolbarTitle())
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        playHistoryAdapter = MusicListAdapter()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = playHistoryAdapter

        viewModel.getPlayHistoryList().observe(
            this
        ) { t ->
            playHistoryAdapter?.list = t
            playHistoryAdapter?.notifyDataSetChanged()
        }
    }

    override fun initData() {
        MusicClientManager.getClient().registerMediaListener(mMusicListener)
        val bundle = Bundle()
        bundle.putInt(MusicListType.TYPE_KEY, playListType)
        MusicClientManager.getClient().sendCustomAction(Actions.ACTION_QUERY_MUSIC_LIST, bundle)
    }

    override fun onDestroy() {
        MusicClientManager.getClient().unregisterMediaListener(mMusicListener)
        super.onDestroy()
    }

    private fun getToolbarTitle(): String {
        val stringArray = resources.getStringArray(R.array.music_list_type)
        return stringArray[playListType]
    }

    inner class MusicListAdapter : RecyclerView.Adapter<MusicListAdapter.PlayHistoryHolder>() {

        var list: List<Music>? = null

        inner class PlayHistoryHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var albumIconImageView: ImageView = itemView.findViewById(R.id.album_icon)
            var songTextView: TextView = itemView.findViewById(R.id.song_textview)
            var singerTextView: TextView = itemView.findViewById(R.id.singer_textview)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayHistoryHolder {
            val itemView =
                LayoutInflater.from(parent.context).inflate(R.layout.item_play_history, null)
            return PlayHistoryHolder(itemView)
        }

        override fun onBindViewHolder(holder: PlayHistoryHolder, position: Int) {
            val music = list?.get(position)
            music?.also {
                holder.albumIconImageView.setImageResource(R.drawable.ic_music_small)

                Glide.with(this@MusicListActivity)
                    .load(it.albumArt)
                    .error(com.jay.media.music.core.R.drawable.ic_music_small)
                    .into(holder.albumIconImageView)
                holder.songTextView.text = it.title
                holder.singerTextView.text = it.artist
            }
        }

        override fun getItemCount(): Int {
            return list?.size ?: 0
        }
    }
}